<?php
namespace Seller\Controller;
use Think\Controller;

/**
 * 广告
 */
class AdController extends ApiController {

    /**
     * 获取广告列表
     */
    public function getadlist(){
        //app请求参数
        $request = array('co_id','page','pagenum');
        //验证
        $param = $this->check_post_key($request);
        //分页
        $limit = (int)($param['page']-1)*$param['pagenum'].','.$param['pagenum'];
        $table = "cs_ad a ,cs_goods g";
        $where = "a.ad_togoods = g.go_id and a.ad_company = ".$param['co_id'];
        $feild = "a.ad_id,a.ad_imgurl,a.ad_togoods,a.ad_day,a.ad_time,a.ad_sdate,g.go_name";
        $order = "ad_id desc";
        //datasel_multi_table 多表查询的一个方法，在function.php
        $result = datasel_multi_table($table,$where,$feild,$order,$limit,1);
        $count = datasel_multi_table($table,$where,$feild,"","",10);
        //返回总条数
        $return['count'] = $count;
        foreach ($result as $key => $val) {
            $arr[] = array(
                "ad_id" => $val['ad_id'],
                "ad_img" => completion_img($val['ad_imgurl'],C('ad_cover')),
                "ad_togoods" => $val['ad_togoods'],
                "go_name" => $val['go_name'],
                "ad_days_Remaining" => days_Remaining($val['ad_sdate']+$val['ad_day']*60*60*24),
            );
        }
        //判断返回数据是否为空
        $return['ad'] = is_null($arr) ? array() : $arr;
        //respons 返回json数据给app
        if($result < 0) $this->respons(10029);
        $this->respons(0,$return);
    }

    /**
     * 添加广告
     */
    public function addad(){
        $request = array('ad_company','ad_imgurl','ad_togoods','ad_day','ad_sdate');
        $param = $this->check_post_key($request);
        $param['ad_state'] = 0;
        $param['ad_time'] = time();
        $param['ad_edate'] = $param['ad_sdate']+(3600*24*$param['ad_day']);
        $sum_points = $param['ad_day']*C('ad_point');
        $param['ad_point'] = $sum_points;
        //p($sum_points);
        //获取积分
        $points = datagetfeild('company','co_id',$param['ad_company'],'co_point');
        if($points >= $sum_points){
            $ad = adddata('ad',$param);
            //扣除积分
            $points_dec = data_set_inc_dec('company',array('co_id'=>$param['ad_company']),'co_point',$sum_points,'setdec');
            //获取当前积分
            $points_total = datagetfeild('company','co_id',$param['ad_company'],'co_point');
            //添加积分记录
            $points_record_data = array(
                'cp_company' => $param['ad_company'],
                'cp_itme' => '广告投放',
                'cp_int' => '-'.$sum_points,
                'cp_surplus' => $points_total,
                'cp_date' => time(),
                'cp_type' => '1',
            );
            $points_record = adddata('cpoint',$points_record_data);
        }else{
            $this->respons(10017);
        }
        if($ad < 0) $this->respons(10049);
        $this->respons(0);
    }

    /**
     * 删除广告
     */
    public function delad(){
        $request = array('ad_id','ad_company');
        $param = $this->check_post_key($request);
        $ad = datadel_where_array('ad',$param);
        if($ad <= 0) $this->respons(10034);
        $this->respons(0);
    }

    /**
     * 获取编辑广告内容
     */
    public function getad(){
        $request = array('ad_id','ad_company');
        $param = $this->check_post_key($request);
        //$ad = datafind_where_array('ad',$param);
        $table = "cs_ad a ,cs_goods g";
        $where = "a.ad_togoods = g.go_id and a.ad_id = ".$param['ad_id']." and ad_company = ".$param['ad_company'];
        $feild = "a.ad_id,a.ad_imgurl,a.ad_togoods,a.ad_day,a.ad_time,g.go_name";
        $ad = datasel_multi_table($table,$where,$feild,"","",20);
        if($ad <= 0) $this->respons(10034);
        $time = $ad['ad_time']+($ad['ad_day']*60*60*24);
        $return = array(
            "ad_id" => $ad['ad_id'],
            "ad_img" => completion_img($ad['ad_imgurl'],C("ad_cover")),
            "ad_togoods" => $ad['ad_togoods'],
            "go_name" => $ad['go_name'],
            "ad_days_Remaining" => days_Remaining($time),
        );
        $this->respons(0,$return);
    }

    /**
     * 编辑广告
     */
    public function modifyad(){
        $request = array('ad_id','ad_imgurl','ad_togoods','ad_company');
        $param = $this->check_post_key($request);
        $param['ad_imgurl'] = explodeimg($param['ad_imgurl'],C('img_url'));
        $ad = updata_array('ad',$param);
        if($ad < 0) $this->respons(10049);
        $this->respons(0);
    }

    /**
     * 获取商品
     */
    public function getgoodlist(){
        $request = array('co_id','page','$pagenum');
        $param = $this->check_post_key($request);
        $limit = (int)($param['page']-1)*$param['pagenum'].','.$param['pagenum'];
        $where = ' go_company = '.$param['co_id'].' and go_status = 1';
        $result = datasel_multi_table('cs_goods',$where,'go_id,go_name,go_cash,go_img1,go_sales',"go_id desc","",1);
        foreach ($result as $key => $val) {
            $val['go_img'] = completion_img($val['go_img1'],C('goods_cover'));
            unset($val['go_img1']);
            $arr[] = $val;
        }
        $return['count'] = datasel_multi_table('cs_goods',$where,'go_id,go_name,go_cash,go_img1,go_sales',"","",10);
        $return['list'] = $arr?$arr:array();
        $this->respons(0,$return);
    }

}